<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>验证查询 | Elasticsearch: 权威指南 | Elastic</title>
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
</head>
<body>
<div class="main-container">
    <section id="content">
        
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原文地址: <a href="https://www.elastic.co/guide/cn/elasticsearch/guide/current/validating-queries.html" rel="nofollow">https://www.elastic.co/guide/cn/elasticsearch/guide/current/validating-queries.html</a>, 版权归 www.elastic.co 所有<br/>
                            英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/guide/current/validating-queries.html" rel="nofollow">https://www.elastic.co/guide/en/elasticsearch/guide/current/validating-queries.html</a>
                            </div>
                        <!-- start body -->
                  <div class="page_header">
<b>请注意:</b><br>本书基于 Elasticsearch 2.x 版本，有些内容可能已经过时。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch: 权威指南</a></span>
»
<span class="breadcrumb-link"><a href="getting-started.html">基础入门</a></span>
»
<span class="breadcrumb-link"><a href="full-body-search.html">请求体查询</a></span>
»
<span class="breadcrumb-node">验证查询</span>
</div>
<div class="navheader">
<span class="prev">
<a href="combining-queries-together.html">« 组合多查询</a>
</span>
<span class="next">
<a href="sorting.html">排序与相关性 »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="validating-queries"></a>验证查询<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/054_Query_DSL/80_Validating_queries.asciidoc">edit</a>
</h2>
</div></div></div>
<p>查询可以变得非常的复杂，尤其和不同的分析器与不同的字段映射结合时，理解起来就有点困难了。不过 <code class="literal">validate-query</code> API 可以用来验证查询是否合法。</p>
<div class="pre_wrapper lang-sense">
<pre class="programlisting prettyprint lang-sense">GET /gb/tweet/_validate/query
{
   "query": {
      "tweet" : {
         "match" : "really powerful"
      }
   }
}</pre>
</div>
<div class="sense_widget" data-snippet="snippets/054_Query_DSL/80_Validate_query.json"></div>
<p>以上 <code class="literal">validate</code> 请求的应答告诉我们这个查询是不合法的：</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">{
  "valid" :         false,
  "_shards" : {
    "total" :       1,
    "successful" :  1,
    "failed" :      0
  }
}</pre>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="understanding-errors"></a>理解错误信息<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/054_Query_DSL/80_Validating_queries.asciidoc">edit</a>
</h3>
</div></div></div>
<p>为了找出  查询不合法的原因，可以将 <code class="literal">explain</code> 参数 加到查询字符串中：</p>
<div class="pre_wrapper lang-sense">
<pre class="programlisting prettyprint lang-sense">GET /gb/tweet/_validate/query?explain <a id="CO26-1"></a><i class="conum" data-value="1"></i>
{
   "query": {
      "tweet" : {
         "match" : "really powerful"
      }
   }
}</pre>
</div>
<div class="sense_widget" data-snippet="snippets/054_Query_DSL/80_Validate_query.json"></div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO26-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p><code class="literal">explain</code> 参数可以提供更多关于查询不合法的信息。</p>
</td>
</tr>
</table>
</div>
<p>很明显，我们将查询类型(<code class="literal">match</code>)与字段名称 (<code class="literal">tweet</code>)搞混了：</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">{
  "valid" :     false,
  "_shards" :   { ... },
  "explanations" : [ {
    "index" :   "gb",
    "valid" :   false,
    "error" :   "org.elasticsearch.index.query.QueryParsingException:
                 [gb] No query registered for [tweet]"
  } ]
}</pre>
</div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="understanding-queries"></a>理解查询语句<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/054_Query_DSL/80_Validating_queries.asciidoc">edit</a>
</h3>
</div></div></div>
<p>对于合法查询，使用 <code class="literal">explain</code> 参数将返回可读的描述，这对准确理解 Elasticsearch 是如何解析你的 query 是非常有用的：</p>
<div class="pre_wrapper lang-sense">
<pre class="programlisting prettyprint lang-sense">GET /_validate/query?explain
{
   "query": {
      "match" : {
         "tweet" : "really powerful"
      }
   }
}</pre>
</div>
<div class="sense_widget" data-snippet="snippets/054_Query_DSL/80_Understanding_queries.json"></div>
<p>我们查询的每一个 index 都会返回对应的 <code class="literal">explanation</code> ，因为每一个 index 都有自己的映射和分析器：</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">{
  "valid" :         true,
  "_shards" :       { ... },
  "explanations" : [ {
    "index" :       "us",
    "valid" :       true,
    "explanation" : "tweet:really tweet:powerful"
  }, {
    "index" :       "gb",
    "valid" :       true,
    "explanation" : "tweet:realli tweet:power"
  } ]
}</pre>
</div>
<p>从 <code class="literal">explanation</code> 中可以看出，匹配 <code class="literal">really powerful</code> 的 <code class="literal">match</code> 查询被重写为两个针对 <code class="literal">tweet</code> 字段的 single-term 查询，一个single-term查询对应查询字符串分出来的一个term。</p>
<p>当然，对于索引 <code class="literal">us</code> ，这两个 term 分别是 <code class="literal">really</code> 和 <code class="literal">powerful</code> ，而对于索引 <code class="literal">gb</code> ，term 则分别是 <code class="literal">realli</code> 和 <code class="literal">power</code> 。之所以出现这个情况，是由于我们将索引 <code class="literal">gb</code> 中 <code class="literal">tweet</code> 字段的分析器修改为 <code class="literal">english</code> 分析器。</p>
</div>

</div>
<div class="navfooter">
<span class="prev">
<a href="combining-queries-together.html">« 组合多查询</a>
</span>
<span class="next">
<a href="sorting.html">排序与相关性 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>